<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<script type="text/javascript" src="js/protobuf.js"></script>
<script type="text/javascript">
    if(!window.WebSocket){
        window.WebSocket = window.MozWebsocket;
    }

    if(window.WebSocket){
        socket = new WebSocket("ws://localhost:9096/websocket");
        var Request;
        var Response;

        protobuf.load("./protobuf/DubboServicePro.proto", function (err, root) {
            if (err) throw err;
            Request = root.lookup("org.windwant.protocal.DubboRequest");
            Response = root.lookup("org.windwant.protocal.DubboResponse");
        });

        socket.onmessage = function(event){
            var reader = new FileReader();
            reader.readAsArrayBuffer(event.data);
            reader.onload = function(e){
                var buf = new Uint8Array(reader.result);
                var message = Response.decode(buf);
                document.getElementById("rtext").value = "status: " + message.status + ", msg: " + message.msg;
            }

        }

        socket.onopen = function(event){
            document.getElementById("rtext").value = "socket open";
        }

        socket.onclose = function(event){
            document.getElementById("rtext").value = "socket close";
        }
    }else{
        alert("browser not supported");
    }

    function sendMessage(){
        if(!window.WebSocket){
            return;
        }

        if(socket.readyState == WebSocket.OPEN){
            var request = Request.create({'requestCode': document.getElementById('request_code').value});
            buffer = Request.encode(request).finish();
            socket.send(buffer);
        }else{
            alert('waiting connecting');
        }
    }

</script>
<body>
<label>请求标识：</label>
<input type="text" id="request_code" name="requestCode" value="3" readonly>
<br/>
<!--<label>请求消息：</label>-->
<!--<input type="text" id="message" name="message" value="best prictice">-->
<br/>
<input type="button" onclick="sendMessage()" value="SEND"/>

</br>
</br>
</br>
<textarea id="rtext" style="width: 30%; height: 200px;"/>
</body>
</html>